<?php

namespace app\admin\model\order;

use think\Model;

class OrderItem extends Model
{
    // 表名
    protected $name = 'order_item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'goods_name',
        'goods_image',
        'spec_name'
    ];

    public function goods()
    {
        return $this->belongsTo('app\admin\model\goods\Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function spec()
    {
        return $this->belongsTo('app\admin\model\goods\GoodsSpec', 'spec_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getGoodsNameAttr($value, $data)
    {
        return $this->goods ? $this->goods->title : '';
    }

    public function getGoodsImageAttr($value, $data)
    {
        return $this->goods ? $this->goods->cover_image : '';
    }

    public function getSpecNameAttr($value, $data)
    {
        return $this->spec ? $this->spec->spec_name : '';
    }
}